﻿namespace UFIDA.U9.SM.Ship
{
	using System;
	using System.Collections.Generic;
	using System.Text; 
	using UFSoft.UBF.AopFrame;	
	using UFSoft.UBF.Util.Context;
    using UFIDA.U9.ISV.SM.Proxy;
    using UFSoft.UBF.Business;
    using UFIDA.U9.CBO.SCM.Enums;
    using UFIDA.U9.CBO.SCM.Item;

	/// <summary>
	/// FastShipSV partial 
	/// </summary>	
	public partial class FastShipSV 
	{	
		internal BaseStrategy Select()
		{
			return new FastShipSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class FastShipSVImpementStrategy : BaseStrategy
	{
		public FastShipSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			FastShipSV bpObj = (FastShipSV)obj;
			
			//get business operation context is as follows
			//IContext context = ContextManager.Context	
			
			//auto generating code end,underside is user custom code
			//and if you Implement replace this Exception Code...
			//throw new NotImplementedException();
            if (bpObj.Ships.Count == 0)
                return new List<long>();

            List<long> ships = CreateShips(bpObj.Ships);
            SubmitShips(ships);
            return ships;

		}


        private List<long> CreateShips(List<FastShipDTO> ships)
        {
            List<long> shipids = new List<long>();

            List<UFIDA.U9.ISV.SM.ShipDTOForIndustryChainData> dtoShips = new List<UFIDA.U9.ISV.SM.ShipDTOForIndustryChainData>();
            foreach (FastShipDTO dtoShip4Fast in ships)
            {
                dtoShips.Add(BuildISVShipDTO(dtoShip4Fast));
            }

            CreateShipSVProxy shipSV = new CreateShipSVProxy();
            shipSV.ShipDTOs = dtoShips;

            List<UFIDA.U9.ISV.SM.DocKeyDTOData> results = shipSV.Do();
            if (results != null)
            {
                foreach (UFIDA.U9.ISV.SM.DocKeyDTOData shipresult in results)
                {
                    shipids.Add(shipresult.DocID);
                }
            }

            return shipids;
        }

        private UFIDA.U9.ISV.SM.ShipDTOForIndustryChainData BuildISVShipDTO(FastShipDTO fastShip)
        {
            UFIDA.U9.ISV.SM.ShipDTOForIndustryChainData isvShip = new UFIDA.U9.ISV.SM.ShipDTOForIndustryChainData();

            isvShip.DocNo = fastShip.DocNo;
            isvShip.DocumentType = new UFIDA.U9.CBO.Pub.Controller.CommonArchiveDataDTOData();
            isvShip.DocumentType.ID = fastShip.DocumentType.ID;
            isvShip.BusinessDate = fastShip.BusinessDate;
            isvShip.OrderBy = new UFIDA.U9.CBO.Pub.Controller.CommonArchiveDataDTOData();
            isvShip.OrderBy.ID = fastShip.Customer.ID;
            isvShip.ShipMemo = fastShip.Memo;
            isvShip.IsNoSOShipment = true;
            if (fastShip.SaleDept != null)
            {
                isvShip.SaleDept = new UFIDA.U9.CBO.Pub.Controller.CommonArchiveDataDTOData();
                isvShip.SaleDept.ID = fastShip.SaleDept.ID;
            }
            if (fastShip.Seller != null)
            {
                isvShip.Seller = new UFIDA.U9.CBO.Pub.Controller.CommonArchiveDataDTOData();
                isvShip.Seller.ID = fastShip.Seller.ID;
            }

            isvShip.ShipLines = new List<UFIDA.U9.ISV.SM.ShipLineDTOForIndustryChainData>();
            
            foreach (FastShipLineDTO fastShipLine in fastShip.ShipLines)
            {
                isvShip.ShipLines.AddRange(BuildISVShipLineDTO(fastShipLine));
            }

            return isvShip;
        }

        private List<UFIDA.U9.ISV.SM.ShipLineDTOForIndustryChainData> BuildISVShipLineDTO(FastShipLineDTO fastShipLine)
        {
            List<UFIDA.U9.ISV.SM.ShipLineDTOForIndustryChainData> isvShipLines = new List<UFIDA.U9.ISV.SM.ShipLineDTOForIndustryChainData>();

            UFIDA.U9.ISV.SM.ShipLineDTOForIndustryChainData isvShipLine = new UFIDA.U9.ISV.SM.ShipLineDTOForIndustryChainData();
            isvShipLine.DocLineNo = fastShipLine.DocLineNo;
            isvShipLine.ItemInfo = new UFIDA.U9.CBO.SCM.Item.ItemInfoData();
            isvShipLine.ItemInfo.ItemID = fastShipLine.Item.ID;
            isvShipLine.QtyPriceAmount = fastShipLine.Quantity;
            isvShipLine.PriceUom = new UFIDA.U9.CBO.Pub.Controller.CommonArchiveDataDTOData();
            isvShipLine.PriceUom.ID = fastShipLine.Uom.ID;
            isvShipLine.FinallyPriceTC = fastShipLine.Price;
            isvShipLine.TotalMoneyTC = fastShipLine.Money;
            isvShipLine.TotalNetMoneyTC = fastShipLine.Money;
            if (fastShipLine.Wh != null)
            {
                isvShipLine.WH = new UFIDA.U9.CBO.Pub.Controller.CommonArchiveDataDTOData();
                isvShipLine.WH.ID = fastShipLine.Wh.ID;
            }
            isvShipLine.ShipLineMemo = fastShipLine.Memo;
            isvShipLine.ID = -isvShipLine.DocLineNo;
            isvShipLine.DonationType = fastShipLine.DonationType.Value;
            isvShipLine.BomID = fastShipLine.BOMID;
            if (fastShipLine.BOMOwner != null)
            {
                isvShipLine.BomOwner = new UFIDA.U9.CBO.Pub.Controller.CommonArchiveDataDTOData();
                isvShipLine.BomOwner.ID = fastShipLine.BOMOwner.ID;
            }
            if (isvShipLine.ItemInfo.ItemID > 0)
            {
                ItemMaster itemmaster = ItemMaster.Finder.FindByID(isvShipLine.ItemInfo.ItemID);
                if (itemmaster != null)
                {
                    if(itemmaster.ItemFormAttribute==ItemTypeAttributeEnum.Kit)
                        isvShipLine.ShipTogetherFlag = (int)KITShipModeEnumData.Kit;
                }
            }
            
            isvShipLine.IsNoSOShipment = true;
            if (fastShipLine.LotID > 0)
            {
                isvShipLine.LotInfo = new UFIDA.U9.CBO.SCM.PropertyTypes.LotInfoData();
                isvShipLine.LotInfo.LotMaster = new UFIDA.U9.Base.PropertyTypes.BizEntityKeyData();
                isvShipLine.LotInfo.LotMaster.EntityID = fastShipLine.LotID;
            }

            

            isvShipLines.Add(isvShipLine);

            foreach (FastShipSubLineDTO fastShipSubLine in fastShipLine.ShipSubLines)
            {
                UFIDA.U9.ISV.SM.ShipLineDTOForIndustryChainData isvShipSubLine = new UFIDA.U9.ISV.SM.ShipLineDTOForIndustryChainData();
                isvShipSubLine.ItemInfo = new UFIDA.U9.CBO.SCM.Item.ItemInfoData();
                isvShipSubLine.ItemInfo.ItemID = fastShipSubLine.Item.ID;
                if (fastShipSubLine.Wh != null)
                {
                    isvShipSubLine.WH = new UFIDA.U9.CBO.Pub.Controller.CommonArchiveDataDTOData();
                    isvShipSubLine.WH.ID = fastShipSubLine.Wh.ID;
                }
                isvShipSubLine.QtyPriceAmount = fastShipSubLine.Quantity;
                isvShipSubLine.ParentShipLineID = isvShipLine.ID;
                isvShipSubLine.ParentShipLineNo = isvShipLine.DocLineNo;
                isvShipSubLine.DonationType = isvShipLine.DonationType;
                //isvShipSubLine.BomID = isvShipLine.BomID;
                //isvShipSubLine.BomOwner = isvShipLine.BomOwner;
                isvShipSubLine.ShipTogetherFlag = (int)KITShipModeEnumData.Kit;
                isvShipSubLine.IsNoSOShipment = true;
                isvShipLines.Add(isvShipSubLine);
            }

           
            return isvShipLines;
        }

        private void SubmitShips(List<long> ships)
        {
            using (ISession session = Session.Open())
            {
                //UFIDA.U9.CBO.SCM.Proxy.BatchOperationProxy bp = new UFIDA.U9.CBO.SCM.Proxy.BatchOperationProxy();
                //bp.EntityTypeFullName = Ship.EntityRes.BE_FullName;
                //bp.FieldList = new List<UFIDA.U9.CBO.SCM.BatchFieldAndValueData>();
                //UFIDA.U9.CBO.SCM.BatchFieldAndValueData fieldData = new UFIDA.U9.CBO.SCM.BatchFieldAndValueData();
                //fieldData.FieldName = "Status";
                //fieldData.FieldValue = ShipStateEnum.Approving.Value;
                //bp.FieldList.Add(fieldData);
                //bp.EntityInfoList = new List<UFIDA.U9.CBO.SCM.EntityInfoData>();
                //foreach (long id in ships)
                //{
                //    UFIDA.U9.CBO.SCM.EntityInfoData data = new UFIDA.U9.CBO.SCM.EntityInfoData();
                //    data.ID = id;
                //    Ship ship = Ship.Finder.FindByID(id);
                //    data.SysVer = ship.SysVersion;
                //    bp.EntityInfoList.Add(data);
                //}

                //List<UFIDA.U9.CBO.SCM.EntityBatchResultData> result = bp.Do();

                //string errmess = "";
                //if (result != null && result.Count > 0)
                //{
                //    foreach (UFIDA.U9.CBO.SCM.EntityBatchResultData ed in result)
                //    {
                //        if (!string.IsNullOrEmpty(ed.ErrorMessage))
                //        {
                //            errmess = string.Concat(errmess, string.Format("{0}:{1} \r\n", ed.EntityID, ed.ErrorMessage));
                //        }
                //    }
                //}
                //if (!string.IsNullOrEmpty(errmess))
                //{

                //    Exception excep = new Exception();
                //    throw new System.ServiceModel.FaultException<Exception>(excep, errmess.ToString());
                //}

                foreach (long id in ships)
                {
                    Ship ship = Ship.Finder.FindByID(id);
                    ship.Status = ShipStateEnum.Approving;
                }

                session.Commit();

            }
        }
	}

	#endregion
	
	
}
